package activity.store;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.hutoubenjg.R;

import java.util.Observable;
import java.util.Observer;

import base.BaseActivity;
import bean.OrderDetailBean;
import presenter.DingDanPresenter;
import util.utls.ByAlert;
import util.utls.Const;
import util.utls.IsUtilUid;
import util.utls.MyGongJv;
import util.utls.SPUtils;
import util.utls.UIAlertView;
import util.utls.XImageLoader;
import util.volley.ResultEntity;

/**
 * 订单详情
 */
public class OrderDetailActivity extends BaseActivity implements Observer {

    ImageView back;
    TextView tvTitle;
    String orderNo;
    DingDanPresenter dingDanPresenter;
    TextView order_detail_order_code, txt_order_detail_order_status,zongji, txt_order_detail_goodstotal_price, txt_order_detail_total_price, txt_order_detail_yunfei;
    String uid;
    LinearLayout container;
    String sid;

    LinearLayout bill_layout, bill_shuihao_layout;//发票布局,税号布局
    TextView bill_title, bill_shuihao;//抬头，税号
    TextView bill_bukai;//补开发票
    String tel;
    private UIAlertView uiAlertView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail);
        tvTitle.setText(getString(R.string.orderdetail));
        orderNo = getIntent().getStringExtra("order_code");
        tel = (String) SPUtils.get(this, null, Const.TEL, "");
        sid=SPUtils.get(this,null,Const.S_ID,"")+"";
        dingDanPresenter = new DingDanPresenter(this);
        uid = SPUtils.get(this, null, Const.ID, "") + "";
        dingDanPresenter.orderDetail(uid, orderNo,sid);
    }

    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.bill_bukai:
                uiAlertView = new UIAlertView(this, "温馨提示！", "是否拨打客服电话？", "是", "取消");
                uiAlertView.show();
                uiAlertView.setClicklistener(new UIAlertView.ClickListenerInterface() {
                    @Override
                    public void doLeft() {
                        try {
                            if (tel != null) {
                                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
                                        + tel));
                                startActivity(intent);
                                uiAlertView.dismiss();
                            }else{
                                ByAlert.alert("暂未获取到客服信息");
                            }

                        } catch (SecurityException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void doRight() {
                        uiAlertView.dismiss();
                    }
                });
                break;
        }

    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            IsUtilUid.tianjia(entity);
            if (entity.getEventType() == DingDanPresenter.daingdan_xiangqing_success) {
                OrderDetailBean orderDetailBean = (OrderDetailBean) entity.getData();
                if (orderDetailBean != null) {
                    double totalGoodsPrice = 0;
                    for (int i = 0; i < orderDetailBean.getItems().size(); i++) {
                        double price = Double.parseDouble(orderDetailBean.getItems().get(i).getPrice()) / 100;
                        View view = LayoutInflater.from(this).inflate(R.layout.item_orderdetail, null);
                        ImageView iv_adapter_list_pic= (ImageView) view.findViewById(R.id.iv_adapter_list_pic);
                        TextView goodsName = (TextView) view.findViewById(R.id.tv_intro);
                        TextView goodsPrice = (TextView) view.findViewById(R.id.tv_price);
                        TextView goodsNum = (TextView) view.findViewById(R.id.tv_num);
                        goodsName.setText(orderDetailBean.getItems().get(i).getName());
                        goodsPrice.setText("¥" + price);
                        totalGoodsPrice += price;
                        XImageLoader.load(Const.IMAGE_HEAD+orderDetailBean.getItems().get(i).getImg(),iv_adapter_list_pic);
                        goodsNum.setText(orderDetailBean.getItems().get(i).getCount());
                        container.addView(view);

                    }
                    order_detail_order_code.setText(orderDetailBean.getOrder_code());
                    txt_order_detail_goodstotal_price.setText("¥" + totalGoodsPrice);

                    if (!orderDetailBean.getScore().equals("0")){
                        zongji.setText("使用"+orderDetailBean.getScore()+"积分    抵扣"+MyGongJv.zhuanhua(Double.parseDouble(orderDetailBean.getScore_pay()) / 100)+"元");
                        txt_order_detail_total_price.setText("¥" + MyGongJv.zhuanhua(Double.parseDouble(orderDetailBean.getPrice()) / 100-(Double.parseDouble(orderDetailBean.getScore_pay()) / 100)));
                    }else{
                        txt_order_detail_total_price.setText("¥" + MyGongJv.zhuanhua(Double.parseDouble(orderDetailBean.getPrice()) / 100));
                    }

                    if (orderDetailBean.getBill_type().equals("0")) {
                        bill_layout.setVisibility(View.GONE);
                    } else if (orderDetailBean.getBill_type().equals("1")) {
                        bill_layout.setVisibility(View.VISIBLE);
                        bill_shuihao_layout.setVisibility(View.GONE);
                    } else if (orderDetailBean.getBill_type().equals("2")) {
                        bill_layout.setVisibility(View.VISIBLE);
                        bill_shuihao_layout.setVisibility(View.VISIBLE);
                    }
                    bill_title.setText(orderDetailBean.getBill_title());
                    bill_shuihao.setText(orderDetailBean.getBill_code());
                    int type = Integer.parseInt(orderDetailBean.getQuery_status());
                    if (type == 1) {//待付款
                        txt_order_detail_order_status.setText("待付款");
                    } else if (type == 2) {//待发货
                        txt_order_detail_order_status.setText("待发货");
                    } else if (type == 3) {//待收货
                        txt_order_detail_order_status.setText("待收货");
                    } else if (type == 4 || type == 7) {//已完成
                        txt_order_detail_order_status.setText("已完成");
                        if (orderDetailBean.getBill_type().equals("0")){
                            bill_bukai.setVisibility(View.VISIBLE);
                        }
                    } else if (type == 8) {//关闭的
                        txt_order_detail_order_status.setText("已关闭");
                    }
                }

            } else if (entity.getEventType() == DingDanPresenter.daingdan_xiangqing_fail) {
                ByAlert.alert("获取订单失败");
            }
        }
    }
}
